
cmake_minimum_required(VERSION 3.16)
set(CMAKE_SYSTEM_PROCESSOR arm)
project(cygia_ecb C ASM)
message(STATUS "Building project: ${PROJECT_NAME}")

# application srcs
file(GLOB_RECURSE APP_SRCS ecb_a03/*.c)

# nuc980 library srcs
file(GLOB_RECURSE LIB_NUC980_SRCS lib-nuc980/*.c)

# rt-thread srcs
file(GLOB_RECURSE RTTHREAD_SRCS rt-thread/src/*.c)
list(REMOVE_ITEM RTTHREAD_SRCS 
    ${PROJECT_SOURCE_DIR}/rt-thread/src/scheduler_mp.c
    ${PROJECT_SOURCE_DIR}/rt-thread/src/slab.c
    ${PROJECT_SOURCE_DIR}/rt-thread/src/cpu.c
)
set_source_files_properties(${RTTHREAD_SRCS} PROPERTIES COMPILE_OPTIONS -D__RT_KERNEL_SOURCE__)

# rt-thread arm926-ej-s port srcs
file(GLOB_RECURSE RTTHREAD_CPU_SRCS rt-thread/libcpu/arm/arm926/*.c)
list(APPEND RTTHREAD_CPU_SRCS
    ${PROJECT_SOURCE_DIR}/rt-thread/libcpu/arm/arm926/context_gcc.S
    ${PROJECT_SOURCE_DIR}/rt-thread/libcpu/arm/arm926/start_gcc.S
    ${PROJECT_SOURCE_DIR}/rt-thread/libcpu/arm/common/div0.c
    ${PROJECT_SOURCE_DIR}/rt-thread/libcpu/arm/common/showmem.c
)

# rt-thread ipc srcs
file(GLOB_RECURSE RT_COMPONENT_IPC_SRCS rt-thread/components/drivers/ipc/*.c)
set_source_files_properties(${RT_COMPONENT_IPC_SRCS} PROPERTIES COMPILE_OPTIONS -D__RT_IPC_SOURCE__)

# lwip srcs
file(GLOB RT_COMPONENT_LWIP_SRCS 
    rt-thread/components/net/lwip/lwip-2.1.2/src/core/*.c
    rt-thread/components/net/lwip/lwip-2.1.2/src/core/ipv4/*.c
    rt-thread/components/net/lwip/lwip-2.1.2/src/api/*.c
    rt-thread/components/net/lwip/port/*.c
    rt-thread/components/net/lwip/lwip-2.1.2/src/apps/ping/ping.c
    rt-thread/components/net/lwip/lwip-2.1.2/src/netif/ethernet.c
    rt-thread/components/net/lwip/lwip-2.1.2/src/netif/lowpan6.c
)
list(REMOVE_ITEM RT_COMPONENT_LWIP_SRCS 
    ${PROJECT_SOURCE_DIR}/rt-thread/components/net/lwip/lwip-2.1.2/src/core/mem.c
)

# rt-thread dfs srcs
file(GLOB_RECURSE RT_COMPONENT_DFS_SRCS
    rt-thread/components/dfs/dfs_v1/src/*.c
    rt-thread/components/dfs/dfs_v1/filesystems/elmfat/*.c
    rt-thread/components/dfs/dfs_v1/filesystems/devfs/*.c
)

# rt-thread drivers srcs
file(GLOB_RECURSE RT_COMPONENT_DRIV_SRCS 
    rt-thread/components/drivers/can/can.c
    rt-thread/components/drivers/core/device.c
    rt-thread/components/drivers/cputime/cputime.c
    rt-thread/components/drivers/cputime/cputimer.c
    rt-thread/components/drivers/hwcrypto/hwcrypto.c
    rt-thread/components/drivers/hwcrypto/hw_hash.c
    rt-thread/components/drivers/hwcrypto/hw_rng.c
    rt-thread/components/drivers/hwcrypto/hw_symmetric.c
    rt-thread/components/drivers/hwtimer/hwtimer.c
    rt-thread/components/drivers/i2c/i2c-bit-ops.c
    rt-thread/components/drivers/i2c/i2c_core.c
    rt-thread/components/drivers/i2c/i2c_dev.c
    rt-thread/components/drivers/misc/adc.c
    rt-thread/components/drivers/pin/pin.c
    rt-thread/components/drivers/misc/rt_drv_pwm.c
    rt-thread/components/drivers/mtd/mtd_nand.c
    rt-thread/components/drivers/rtc/alarm.c
    rt-thread/components/drivers/rtc/rtc.c
    rt-thread/components/drivers/serial/serial.c
    rt-thread/components/drivers/spi/qspi_core.c
    rt-thread/components/drivers/spi/spi_core.c
    rt-thread/components/drivers/spi/spi_dev.c
    rt-thread/components/drivers/usb/usbdevice/class/cdc_vcom.c
    rt-thread/components/drivers/usb/usbdevice/class/mstorage.c
    rt-thread/components/drivers/usb/usbdevice/core/*.c
    rt-thread/components/drivers/usb/usbhost/class/mass.c
    rt-thread/components/drivers/usb/usbhost/class/udisk.c
    rt-thread/components/drivers/usb/usbhost/core/*.c
    rt-thread/components/drivers/watchdog/watchdog.c
)

# rt-thread finsh srcs
file(GLOB_RECURSE RT_COMPONENT_FINSH_SRCS rt-thread/components/finsh/*.c)

# rt-thread libc srcs
file(GLOB_RECURSE RT_COMPONENT_LIBC_SRCS
    rt-thread/components/libc/compilers/common/*.c
    rt-thread/components/libc/compilers/newlib/*.c
    rt-thread/components/libc/posix/io/poll/*.c
)

file(GLOB_RECURSE RT_COMPONENT_NET_SRCS
    rt-thread/components/net/netdev/src/*.c
    rt-thread/components/net/sal/dfs_net/dfs_net.c
    rt-thread/components/net/sal/impl/af_inet_lwip.c
    rt-thread/components/net/sal/socket/*.c
    rt-thread/components/net/sal/src/*.c
)

set(RT_COMPONENT_UTIL_SRCS
    rt-thread/components/utilities/libadt/avl/avl.c
    rt-thread/components/utilities/utest/utest.c
)

set(NU_PACKAGE_SRCS
    nu_packages/SPINAND/drv_spinand.c
    nu_packages/SPINAND/spinand.c
)

file(GLOB_RECURSE RAMDISK_SRCS
    packages/ramdisk/*.c
)

file(GLOB YAFFS2_SRCS
    packages/yaffs2/*.c
    packages/yaffs2/yaffs/*.c
    packages/yaffs2/yaffs/direct/*.c
)


set(APP_TARGET_NAME cygia_ecb_a03)
add_executable(${APP_TARGET_NAME} 
    ${APP_SRCS}
    ${LIB_NUC980_SRCS} ${NU_PACKAGE_SRCS} ${RTTHREAD_SRCS} ${RTTHREAD_CPU_SRCS}
    ${RT_COMPONENT_DFS_SRCS} ${RT_COMPONENT_DRIV_SRCS} ${RT_COMPONENT_FINSH_SRCS}
    ${RT_COMPONENT_LIBC_SRCS} ${RT_COMPONENT_NET_SRCS} ${RT_COMPONENT_UTIL_SRCS}
    ${RT_COMPONENT_IPC_SRCS} ${RT_COMPONENT_LWIP_SRCS} ${RAMDISK_SRCS}
)

target_include_directories(${APP_TARGET_NAME} PRIVATE
    ecb_a03
    lib-nuc980/Driver/Include
    lib-nuc980/rtt_port
    lib-nuc980/UsbHostLib/inc
    nu_packages/TPC
    nu_packages/SPINAND
    nu_packages/NuUtils/inc
    rt-thread/include
    rt-thread/libcpu/arm/arm926
    rt-thread/libcpu/arm/common
    rt-thread/components/dfs/dfs_v1/include
    rt-thread/components/dfs/dfs_v1/filesystems/devfs
    rt-thread/components/dfs/dfs_v1/filesystems/elmfat
    rt-thread/components/drivers/include
    rt-thread/components/drivers/hwcrypto
    rt-thread/components/drivers/spi
    rt-thread/components/drivers/usb/usbdevice
    rt-thread/components/drivers/usb/usbhost
    rt-thread/components/drivers/usb/usbhost/core
    rt-thread/components/drivers/usb/usbhost/classs
    rt-thread/components/drivers/usb/usbhost/include
    rt-thread/components/finsh
    rt-thread/components/libc/compilers/common/include
    rt-thread/components/libc/compilers/newlib
    rt-thread/components/libc/posix/io/poll
    rt-thread/components/net/lwip/port
    rt-thread/components/net/lwip/lwip-2.1.2/src/include
    rt-thread/components/net/lwip/lwip-2.1.2/src/include/netif
    rt-thread/components/net/netdev/include
    rt-thread/components/net/sal/include
    rt-thread/components/net/sal/impl
    rt-thread/components/net/sal/include/socket
    rt-thread/components/net/sal/include/dfs_net
    rt-thread/components/net/sal/include/socket/sys_socket
    rt-thread/components/utilities/libadt/avl
    rt-thread/components/utilities/libadt/bitmap
    rt-thread/components/utilities/libadt/hashmap
    rt-thread/components/utilities/libadt/ref
    rt-thread/components/utilities/utest
    packages/ramdisk/inc
    packages/yaffs2/yaffs
    packages/yaffs2/yaffs/direct
)

target_link_options(${APP_TARGET_NAME} PRIVATE
    ${COMMON_FLAGS}
    -T "${PROJECT_SOURCE_DIR}/tool/linker_script/nuc980.ld"
    -Wl,--gc-sections,-cref,-Map=${APP_TARGET_NAME}.map
)

target_compile_options(${APP_TARGET_NAME} PRIVATE $<$<COMPILE_LANGUAGE:ASM>:
    ${COMMON_FLAGS}
    -x assembler-with-cpp
>)

target_compile_options(${APP_TARGET_NAME} PRIVATE $<$<COMPILE_LANGUAGE:C>:
    ${COMMON_FLAGS}
    -D__RTTHREAD__ -DRT_USING_NEWLIBC -DRT_USING_LIBC -D_POSIX_C_SOURCE=1
>)

add_custom_command(TARGET ${APP_TARGET_NAME} POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary ${APP_TARGET_NAME}.elf ${APP_TARGET_NAME}.bin
        COMMAND ${CMAKE_OBJDUMP} -d ${APP_TARGET_NAME}.elf > ${APP_TARGET_NAME}.s
        COMMAND ${CMAKE_SIZE} --format=berkeley ${APP_TARGET_NAME}.elf
        COMMAND ${CMAKE_COMMAND} -E copy ${APP_TARGET_NAME}.bin  ${PROJECT_SOURCE_DIR}/tool/flash_tool/image/kernel.bin
        COMMENT "Generate ${APP_TARGET_NAME} binary..."
)
